﻿'Copyright 2010 Brian L. Crawford

'Licensed under the Apache License, Version 2.0 (the "License");
'you may not use this file except in compliance with the License.
'You may obtain a copy of the License at

'    http://www.apache.org/licenses/LICENSE-2.0

'Unless required by applicable law or agreed to in writing, software
'distributed under the License is distributed on an "AS IS" BASIS,
'WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'See the License for the specific language governing permissions and
'limitations under the License.

'This software uses Microsoft .Net framework used with permission from      
'Microsoft. Microsoft, .Net Framework, Visual Studio, Visual Basic, Visual C++ 
'remain the trademark and copyright of Microsoft and are subject to their 
'individual licenses.


Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.Windows.Forms

Module Threading
    'example on using classes  to pass values to a function running on a seperate thread and how to return a value

    Private Class AreaClass2
        Public Base As Double
        Public Height As Double
        Function CalcArea() As Double
            ' Calculate the area of a triangle.
            Return 0.5 * Base * Height
        End Function
    End Class

    Private WithEvents BackgroundWorker1 As New System.ComponentModel.BackgroundWorker

    Private Sub TestArea2()
        Dim AreaObject2 As New AreaClass2
        AreaObject2.Base = 30
        AreaObject2.Height = 40

        ' Start the asynchronous operation.
        BackgroundWorker1.RunWorkerAsync(AreaObject2)
    End Sub

    ' This method runs on the background thread when it starts.
    Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _
        ByVal e As System.ComponentModel.DoWorkEventArgs) _
        Handles BackgroundWorker1.DoWork

        Dim AreaObject2 As AreaClass2 = CType(e.Argument, AreaClass2)
        ' Return the value through the Result property.
        e.Result = AreaObject2.CalcArea()
    End Sub

    ' This method runs on the main thread when the background thread finishes.
    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
        ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
        Handles BackgroundWorker1.RunWorkerCompleted

        ' Access the result through the Result property.
        Dim Area As Double = CDbl(e.Result)
        MsgBox("The area is: " & Area)
    End Sub


End Module
